require 'watir'
include Watir



class Class_collector

        @ie = nil;
        
        #Starts the browser. If there is already a browser it can attach to
        #then it will not create a new intance
        def browser_startup
                if Watir::IE.find(:title, //) != nil
                        @ie = Watir::IE.find(:title, //)
                else
                        Watir::IE.new
                        Watir::Wait.until(){@ie.javascript_dialog.button("OK").exists?}
                        @ie.javascript_dialog.button("No").click
                        @ie = Watir::IE.find(:title, //)
                end
                self.main
        end
        
        #method
        def main
                #Insert username and password
                user_name = "USERNAME"
                user_password = "PASSWORD"
                
                self.logout
                @ie = Watir::IE.find(:title, //);
                @ie.goto("registrar.utexas.edu/schedules");
                @ie.div(:class, "callout2 clearfix").li(:index, 0).link(:index, 0).click
                @ie.link(:text, "Find courses now").click


                #Log in to schedule
                Watir::Wait.until(){@ie.text_field(:name, "LOGON").exists?}
                @ie.text_field(:name, "LOGON").value = user_name
                @ie.text_field(:name, "PASSWORDS").value = user_password
                Watir::Wait.until(){@ie.button(:type, "submit").exists?}
                @ie.button(:type, "submit").click_no_wait
                Watir::Wait.until(){@ie.javascript_dialog.button("OK").exists?}
                @ie.javascript_dialog.button("OK").click
                sleep 1

                #Select every single list
                Watir::Wait.until(){@ie.select_list(:id, "s_fos_fl").exists?}
                contents = @ie.select_list(:id, "s_fos_fl").getAllContents
                prev_url = @ie.url
                contents.shift
                class_level = ["level.lower", "level.upper", "level.grad"]
                class_level.each do |y|
                        contents.each do |x|
                                @ie.checkbox(:id, y).click
                                Watir::Wait.until(){@ie.select_list(:id, "s_fos_fl").exists?}
                                @ie.select_list(:id, "s_fos_fl").set(x)
                                #@ie.select_list(:id, "s_fos_fl").set(contents[2])
                                @ie.link(:href, /javascript/).click
                                #This gets the whole String of all class to parse
                                if @ie.table(:id, "classList").exists?
                                  #aFile = File.new("test.txt", "w+")
                                  @ie.table(:id, "classList").each do |course|
                                     if course.text !~ /Unique/ && course.column_count == 7
                                        (0..6).each do |i|
                                           row_text = course.td(:index, i).text
                                           case i
                                           when 0
                                              puts "UNIQUE: "+row_text
                                           when 1
                                              if row_text =~ /(\w+)[\s+\r\n]*(\w*)/
                                                 puts "DAYS: "+$1+" and "+$2
                                              end
                                           when 2
                                              if row_text =~ /(\w+\sto\s\s\s\w+)[\s+\r\n]*([\w+\sto\s\s\s\w+]*)/
                                                 puts "HOURS: "+$1+" "+$2
                                              end
                                           when 3
                                              puts "ROOM: "+row_text
                                           when 4
                                              puts "INSTRUCTOR: "+row_text
                                           when 5
                                              puts "STATUS: "+row_text
                                           when 6
                                              puts "FLAGS: "+row_text
                                           end
                                        end
                                     end
                                  end
										
                                  #aFile.syswrite(text)
                                else
                                        @ie.goto(prev_url)
                                end
                                
                                while @ie.link(:text, "next").exists?
                                        @ie.link(:text, "next").click
                                        @ie.table(:id, "classList").text
                                end
                                
                                @ie.goto(prev_url)
                        end
               end

                
                logout
        end
        
        #Logout
        def logout
                if @ie.div(:id, "ut_head").link(:text, "LOGOFF").exists?
                        @ie.div(:id, "ut_head").link(:text, "LOGOFF").click_no_wait
                        Watir::Wait.until(){@ie.javascript_dialog.button("OK").exists?}
                        @ie.javascript_dialog.button("OK").click
                end
        end
end

a = Class_collector.new
a.browser_startup